package com.sharding.shardingdemo.utils;


import lombok.Data;
import lombok.NoArgsConstructor;


@Data
@NoArgsConstructor
public class ResultData<T> {
    private boolean success;

    private Integer code;

    private String message;

    private T data;

    public ResultData(ResultCode code) {
        this.success = code.success;
        this.code = code.code;
        this.message = code.message;
    }

    public ResultData<T> success(T data) {
        this.success = ResultCode.SUCCESS.success;
        this.code = ResultCode.SUCCESS.code;
        this.message = ResultCode.SUCCESS.message;
        this.setData(data);
        return this;
    }

    public ResultData<T> success(ResultCode code, T data) {
        this.success = code.success;
        this.code = code.code;
        this.message = code.message;
        this.setData(data);
        return this;
    }

    public ResultData<T> error(T data) {
        this.success = ResultCode.FAIL.success;
        this.code = ResultCode.FAIL.code;
        this.message = ResultCode.FAIL.message;
        this.setData(data);
        return this;
    }


    public ResultData(Integer code, String message, boolean success) {
        this.code = code;
        this.message = message;
        this.success = success;
    }



    public static ResultData SUCCESS() {
        return new ResultData(ResultCode.SUCCESS);
    }

    public static ResultData ERROR() {
        return new ResultData(ResultCode.SERVER_ERROR);
    }

    public static ResultData FAIL() {
        return new ResultData(ResultCode.FAIL);
    }

    public static ResultData ERROR_DEFINE(Integer code, String message) {
        return new ResultData(code, message, false);
    }
    public static ResultData ERROR_DEFINE(ResultCode resultCode) {
        return new ResultData(resultCode.code, resultCode.message, false);
    }


}
